package com.zzjson.strategy.provider;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ApplicationBootstrap {
    private static final Logger logger = LoggerFactory.getLogger(ApplicationBootstrap.class);

    public static final ReentrantLock LOCK = new ReentrantLock();

    public static final Condition STOP = LOCK.newCondition();

    static final String[][] applicationModes = {
            {"dev"},
            {"pro"},
            {"pro-file"}
    };

    public static void main(String[] args) throws IOException {
        String[] activeProfiles = applicationModes[0];
        if (args != null && args.length > 0) {
            try {
                int mode = Integer.valueOf(args[0]);
                if (mode > 0 && mode < applicationModes.length) {
                    activeProfiles = applicationModes[mode];
                }
            } catch (Exception ex) {

            }

        }
        logger.info("Active profiles {}", Arrays.toString(activeProfiles));
        @SuppressWarnings("resource")
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext();
        ctx.setConfigLocations("classpath:/config/applicationContext.xml");
        ctx.getEnvironment().setActiveProfiles(activeProfiles);
        ctx.refresh();
        ctx.registerShutdownHook();
        try {
            LOCK.lock();
            STOP.await();
        } catch (InterruptedException e) {
            logger.warn("Dubbo service server stopped, interrupted by other thread!", e);
        } finally {
            LOCK.unlock();
        }


    }

}
